﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using AnyOffice.BIZ;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.meeting.manage
{
    public partial class record :ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

      
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("knowledge.aspx?path=" + Server.UrlEncode(this.ViewState["parentPath"].ToString()));
        }
        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件添加", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件删除", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["OldAttachmentId"].ToString() != string.Empty)
                {
                    AttachmentBiz.DeleteByAttachmendId(base.Conn, this.ViewState["OldAttachmentId"].ToString());
                }
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 3;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("表单提交 - 附件上传相关", exception);
                }
            }
            string commandText = "UPDATE [dbo].[Meeting] SET [RecordReader] = @RecordReader, [AttachmentId]=@AttachmentId, [RecordContent]=@RecordContent WHERE [MeetingId] = @MeetingId";
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@MeetingId", SqlDbType.Int);
            commandParameters[0].Value = Convert.ToInt32(this.ViewState["MeetingId"].ToString());
            commandParameters[1] = new SqlParameter("@RecordReader", SqlDbType.NText);
            commandParameters[1].Value = this.ToId.Value;
            commandParameters[2] = new SqlParameter("@AttachmentId", SqlDbType.NVarChar, 50);
            if (attachmentId != string.Empty)
            {
                commandParameters[2].Value = attachmentId;
            }
            else
            {
                commandParameters[2].Value = DBNull.Value;
            }
            commandParameters[3] = new SqlParameter("@RecordContent", SqlDbType.NText);
            commandParameters[3].Value = this.txtContent.Value;
            SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, commandText, commandParameters);
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPromptClose("会议纪要发布成功！");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["meeting_id"] != null) ? int.Parse(Request.QueryString["meeting_id"].ToString()) : 0;
                this.ViewState["MeetingId"] = num;
                string commandText = "SELECT [Name],[RecordReader],[AttachmentId],[RecordContent], dbo.GetUserNames([RecordReader]) AS RecordReaderName FROM [dbo].[Meeting] WHERE [MeetingId] = @MeetingId";
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@MeetingId", SqlDbType.Int) };
                commandParameters[0].Value = num;
                DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.litName.Text = table.Rows[0]["Name"].ToString();
                    if (table.Rows[0]["RecordReader"] != DBNull.Value)
                    {
                        this.ToId.Value = table.Rows[0]["RecordReader"].ToString();
                    }
                    if (table.Rows[0]["RecordReaderName"] != DBNull.Value)
                    {
                        this.ToName.Text = table.Rows[0]["RecordReaderName"].ToString();
                    }
                    if (table.Rows[0]["RecordContent"] != DBNull.Value)
                    {
                        this.txtContent.Value = table.Rows[0]["RecordContent"].ToString();
                    }
                    string attachmentId = (table.Rows[0]["AttachmentId"] != DBNull.Value) ? table.Rows[0]["AttachmentId"].ToString() : "";
                    this.ViewState["FileNums"] = 0;
                    this.ViewState["FileList"] = new ArrayList();
                    this.ViewState["UniqueTempPath"] = "";
                    this.ViewState["TotalSize"] = 0;
                    this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                    this.ViewState["OldAttachmentId"] = attachmentId;
                    if (attachmentId != string.Empty)
                    {
                        DataTable table2 = new AttachmentDao().SelectByAttachmentId(base.Conn, attachmentId);
                        if (table2.Rows.Count > 0)
                        {
                            try
                            {
                                this.ViewState["FileNums"] = table2.Rows.Count;
                                ArrayList list = new ArrayList();
                                string savedPath = AttachmentBiz.GetSavedPath(attachmentId);
                                string uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                                this.ViewState["UniqueTempPath"] = uniqueTempPath;
                                AttachmentBiz.Copy(savedPath, uniqueTempPath);
                                int num2 = 0;
                                for (int i = 0; i < table2.Rows.Count; i++)
                                {
                                    list.Add(table2.Rows[i]["Name"].ToString());
                                    num2 += Convert.ToInt32(table2.Rows[i]["TotalSize"].ToString());
                                }
                                this.ltbFileList.DataSource = list;
                                this.ltbFileList.DataBind();
                                this.ltbFileList.Rows = table2.Rows.Count;
                                this.ViewState["FileList"] = list;
                                this.ViewState["TotalSize"] = num2;
                                this.pnlAttach.Visible = true;
                            }
                            catch (Exception exception)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("提交前 - 附件上传相关", exception);
                                }
                            }
                        }
                    }
                    this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                }
                else
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("无此会议");
                }
            }
        }

 

    }
}
